﻿'    Copyright (C) 2009-2010  Ryan Skeldon <psykad@gmail.com>
'
'    This program is free software; you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation; either version 2 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program; if not, write to the Free Software
'    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Option Explicit On
Imports System.Net
Imports System.Net.Sockets

Namespace Objects
    Public Class IClient

        Public Sub New(ByVal ClientSocket As Socket)
            _socket = ClientSocket
        End Sub

#Region "Properties"
        Private _socket As Socket
        Public ReadOnly Property Socket() As Socket
            Get
                Return _socket
            End Get
        End Property

        Private _removeFlag As Boolean
        Public Property RemoveFlag() As Boolean
            Get
                Return _removeFlag
            End Get
            Set(ByVal value As Boolean)
                _removeFlag = value
            End Set
        End Property

        Private _sendQueue As Queue
        Public ReadOnly Property SendQueue() As Queue
            Get
                If _sendQueue Is Nothing Then
                    _sendQueue = New Queue
                End If
                Return _sendQueue
            End Get
        End Property

        Public ReadOnly Property HasData() As Boolean
            Get
                Return _socket.Poll(10, SelectMode.SelectRead)
            End Get
        End Property

        Public ReadOnly Property IsConnected() As Boolean
            Get
                Dim SocketConnected As Boolean

                Try
                    SocketConnected = Not (Socket.Poll(1, SelectMode.SelectRead) And (Socket.Available = 0))
                Catch
                End Try
                Return SocketConnected
            End Get
        End Property
#End Region

#Region "Public Methods"
        Public Sub Send(ByVal Message As String)
            Try
                If _socket IsNot Nothing Then
                    If IsConnected() Then
                        Dim DataOut() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(Message & vbCrLf)
                        _socket.Send(DataOut, DataOut.Length, SocketFlags.None)
                    End If
                End If
            Catch ex As Exception

            End Try
        End Sub
#End Region
    End Class
End Namespace